package cz.muni.fi.pa165.dundd.web;

import cz.muni.fi.pa165.dundd.dto.TroopDTO;
import cz.muni.fi.pa165.dundd.service.TroopService;
import static cz.muni.fi.pa165.dundd.web.BaseActionBean.escapeHTML;
import java.util.List;
import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author xszanto1, xceluch
 */
@UrlBinding("/troops/{$event}/{troop.id}")
public class TroopActionBean extends BaseActionBean implements ValidationErrorHandler {

    final static Logger log = LoggerFactory.getLogger(TroopActionBean.class);

    @SpringBean
    TroopService troopService;

    private List<TroopDTO> troops;

    @ValidateNestedProperties(value = {
        @Validate(on = {"add", "save"}, field = "name", required = true),
        @Validate(on = {"add", "save"}, field = "mission", required = true),
    })
    private TroopDTO troop;

    public TroopDTO getTroop() {
        return troop;
    }

    public void setTroop(TroopDTO troop) {
        this.troop = troop;
    }

    public Resolution add() {
        troopService.createTroop(troop);
        getContext().getMessages().add(new LocalizableMessage("troop.add.message", escapeHTML(troop.getName()), escapeHTML(troop.getMission())));
        return new RedirectResolution(this.getClass(), "list");
    }

    @DefaultHandler
    public Resolution list() {
        troops = troopService.retrieveAllTroops();
        return new ForwardResolution("/troop/list.jsp");
    }

    public List<TroopDTO> getTroops() {
        return troops;
    }

    public Resolution delete() {
        //only id is filled by the form
        troop = troopService.retrieveTroopById(troop.getId());
        System.out.println(troop.getId());
        troopService.deleteTroop(troop);
        getContext().getMessages().add(new LocalizableMessage("troop.delete.message",
                escapeHTML(troop.getName()), escapeHTML(troop.getMission())));
        return new RedirectResolution(this.getClass(), "list");
    }


    @Override
    public Resolution handleValidationErrors(ValidationErrors errors) throws Exception {
        //fill up the data for the table if validation errors occured
        //return null to let the event handling continue
        troops = troopService.retrieveAllTroops();
        return null;
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadTroopFromDatabase() {
        String ids = getContext().getRequest().getParameter("troop.id");
        if (ids == null) {
            return;
        }
        troop = troopService.retrieveTroopById(Long.parseLong(ids));
    }

    public Resolution edit() {
        return new ForwardResolution("/troop/edit.jsp");
    }

    public Resolution save() {
        troopService.updateTroop(troop);
        return new RedirectResolution(this.getClass(), "list");
    }
}
